package com.cysyz.web.modules.base.user.domain;


import com.cysyz.mylib.domain.CommonUser;
import com.cysyz.mylib.utils.MD5;
import com.cysyz.mylib.utils.MyStringUtils;
import com.cysyz.web.modules.base.lock.LockAble;
import com.cysyz.web.modules.base.lock.LockStatus;
import com.cysyz.web.modules.base.permission.domain.SysUserRole;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * SysUser entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "FNM_SYS_USER", uniqueConstraints = @UniqueConstraint(columnNames = "LOGIN_NAME"))
public class SysUser extends CommonUser implements LockAble {

	//alias
	public static final String TABLE_ALIAS = "SysUser";

	public static final String ALIAS_ROLE_HAS_THIS_USER = "roleHasThisUser";

	// Fields
	@Transient
	private Boolean roleHasThisUser;


	@Override
	public String[] businessKeys() {
		return new String[]{"loginName"};
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysUser")
	private Set<SysUserRole> sysUserRoles = new HashSet<SysUserRole>(0);

	public Set<SysUserRole> getSysUserRoles() {
		return this.sysUserRoles;
	}

	public void setSysUserRoles(Set<SysUserRole> sysUserRoles) {
		this.sysUserRoles = sysUserRoles;
	}

	public Boolean getRoleHasThisUser() {
		return roleHasThisUser;
	}

	public void setRoleHasThisUser(Boolean roleHasThisUser) {
		this.roleHasThisUser = roleHasThisUser;
	}

	public void refreshStatsAboutLogin (String loginIp) {
		this.setLastLoginIp(loginIp);
		this.setLastLoginTime(new Date());
		this.setLoginCount(this.getLoginCount() + 1);
	}

	/**
	 * 利用MD5对密码进行加密
	 */
	public void md5Password() {
		if (MyStringUtils.hasText(this.getPassword())) {
			this.setPassword(MD5.MD5Encode(this.getPassword()));
		}
	}


	@Enumerated
	@Column(name = "LOCK_STATUS")
	private LockStatus lockStatus;

	@Override
	public LockStatus getLockStatus() {
		return lockStatus;
	}

	@Override
	public void setLockStatus(LockStatus lockStatus) {
		this.lockStatus = lockStatus;
	}
}